<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui.admin/css/style.css" />
<style type="text/css">

</style>
<title>二维码打印</title>
<style type="text/css">
	#qrcodes{
		margin: 10px;
	}
	.qrcode{
		float: left;
		margin-bottom: 20px;
		position: relative;
	}
	.qrcode> img{
		width: 75px;
		height: 75px;
		
	}
	.qrcode> p{
		display: inline-block;
		width: 75px;
		font-size: 12px;
		text-align: center;
		height: 20px;
		position: absolute;
		bottom: -25px;
		left: 1px;
	}
	@page {
      size: auto;  /* auto is the initial value */
      margin: 20px 20px 20px 25px; /* this affects the margin in the printer settings */
    }
</style>
</head>
<body>
	<div style="margin: 10px auto; "><button class="btn btn-success" type="submit" >打印</button>
	</div>
	<div id="qrcodes">
	
	</div>
	
	
	
	
	<script type="text/javascript">
		$(function() {
			$(".btn-width").click(function() {
				layer.prompt({
					formType: 0,
				  	value: '',
				  	title: '调整的宽度'
				}, function(value,index){
					$(".qrcode").css("margin-left",value+ "px")
					$(".qrcode").css("margin-right",value+ "px")
				});
			});
			
			$(".btn-height").click(function() {
				layer.prompt({
					formType: 0,
				  	value: '',
				  	title: '调整的高度'
				}, function(value,index){
					$(".qrcode").css("margin-top",value+ "px")
					$(".qrcode").css("margin-bottom",value+ "px")
				});
			});
			var num = 0;
			
			layer.prompt({
				formType: 0,
			  	value: '',
			  	title: '需要打印多少张?(一次性最多200张)'
			}, function(value,index){
				layer.close(index);
				num = value;
				setTimeout(generateSuccess, 1000);
				for (var i = 0; i < value; i++) {
					
					var params = {
							"qrcodeNum" : 1
					}
					$.post("${ctx}/order/generateList",params  , function(obj) {
						console.log(obj)
						console.log(obj.result)
						if(obj.result){
							var html = '<div class="qrcode"><img src="data:image/jpg;base64,'+obj.qrCodeList[0].qrCode+'"><br> <p>'+obj.qrCodeList[0].checkCode+'</p></div>'
							$("#qrcodes").append(html);
						}
		        	});
				}
			});
			
			var submission = false;
			function generateSuccess() {
				console.log($(".qrcode").length + "  :   " + num);
				if($(".qrcode").length == num){
					layer.msg("生成完毕！");
					submission = true;
					return;
				}
				setTimeout(generateSuccess, 1000);
			}
			printQR();
			function printQR() {
				$(".btn-success").click(function() {
					if(submission){
						var headhtml = "<html><head><title></title></head><body>";
					    var foothtml = "</body>";
					    // 获取div中的html内容
					    // 获取div中的html内容，jquery写法如下
					    var newhtml= $("#qrcodes").html();

					    // 获取原来的窗口界面body的html内容，并保存起来
					    var oldhtml = document.body.innerHTML;

					    // 给窗口界面重新赋值，赋自己拼接起来的html内容
					    document.body.innerHTML =newhtml;
					    // 调用window.print方法打印新窗口
					    if(getExplorer() == "IE"){
							pagesetup_null();
							}
						window.print();
					    // 将原来窗口body的html值回填展示
					    document.body.innerHTML = oldhtml;
					   
						printQR();
					}
				});
				
			}
			
			
			
			
			

				function pagesetup_null(){                
				    var hkey_root,hkey_path,hkey_key;
				    hkey_root="HKEY_CURRENT_USER";
				    hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
				    try{
				        var RegWsh = new ActiveXObject("WScript.Shell");
				        hkey_key="header";
				        RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
				        hkey_key="footer";
				        RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
				    }catch(e){}
				}

				function getExplorer() {
				    var explorer = window.navigator.userAgent ;
				    //ie 
				    if (explorer.indexOf("MSIE") >= 0) {
				        return "IE";
				    }
				    //firefox 
				    else if (explorer.indexOf("Firefox") >= 0) {
				        return "Firefox";
				    }
				    //Chrome
				    else if(explorer.indexOf("Chrome") >= 0){
				        return "Chrome";
				    }
				    //Opera
				    else if(explorer.indexOf("Opera") >= 0){
				        return "Opera";
				    }
				    //Safari
				    else if(explorer.indexOf("Safari") >= 0){
				        return "Safari";
				    }
				}
		});
	
	</script>
</body>
</html>
